package org.dicr.util.jds.conn.gui;

import java.awt.*;

import javax.swing.*;

import org.dicr.util.jds.conn.*;

/***********************************************************************************************************************
 * Отображатель элементов списка соединений.
 * 
 * @author Igor A Tarasov me@dicr.dn.ua
 * @version 030103
 **********************************************************************************************************************/
public class ConnectionsListCellRenderer extends DefaultListCellRenderer {
	private static final long serialVersionUID = 1L;

	private Font normalFont = null;

	private Font boldFont = null;

	private boolean inited = false;

	private ConnectionsFactory factory = null;

	/*******************************************************************************************************************
	 * Конструктор
	 ******************************************************************************************************************/
	public ConnectionsListCellRenderer() {
		super();
	}

	/*******************************************************************************************************************
	 * Установка менеджера соединений
	 * 
	 * @param theFactory connection factory
	 ******************************************************************************************************************/
	public void setFactory(ConnectionsFactory theFactory) {
		this.factory = theFactory;
	}

	/*******************************************************************************************************************
	 * Возвращает менеджер соединений
	 * 
	 * @return connection factory
	 ******************************************************************************************************************/
	public ConnectionsFactory getFactory() {
		return this.factory;
	}

	/**
	 * @see javax.swing.DefaultListCellRenderer#getListCellRendererComponent(javax.swing.JList, java.lang.Object, int,
	 *      boolean, boolean)
	 */
	@Override
	public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
		if (!this.inited) {
			Font listfont = list.getFont();
			this.normalFont = new Font(listfont.getName(), Font.PLAIN, listfont.getSize());
			this.boldFont = new Font(listfont.getName(), Font.BOLD, listfont.getSize());
			this.inited = true;
		}
		super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
		if (value != null && value instanceof ConnectionElement) {
			ConnectionElement elem = (ConnectionElement) value;
			setFont((this.factory != null && elem.getID() == this.factory.getDefaultConnectionID()) ? this.boldFont
			        : this.normalFont);
			this.setText(elem.getName() + "  (" + elem.getDescriptor().getConnectionURL() + ")");
		}
		return this;
	}
}
